package com.easydev.basic.web.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * filter示例
 * 
 * @author glf
 * @version 1.0
 * @CreateDate 2016年3月17日
 */
public class DemoFilter implements Filter {

    private Logger log = LoggerFactory.getLogger(DemoFilter.class);

    private FilterConfig config = null;

    /**
     * 该方法会在容器启动时执行
     * 
     * @author glf
     * @param filterConfig
     * @throws ServletException
     */
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        log.info("DemoFilter init执行");
        this.config = filterConfig;
    }

    /**
     * 该方法会在业务处理器处理请求之前被调用<br/>
     * 此处可以执行一些操作,例如替换默认的request和response
     * 
     * @author glf
     * @param request
     * @param response
     * @param chain
     * @throws IOException
     * @throws ServletException
     */
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        log.info("DemoFilter doFilter执行");
        // config可以获取上下文对象，获取filter配置参数
        config.getServletContext();
        log.info("filter配置参数为：" + config.getInitParameter("param"));
        // 开始下一个filter，或者进入下一个流程
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {
        log.info("DemoFilter destroy执行");
    }

}
